#include<iostream>
#include<vector>
#include<sstream>
#include<cstring>
using namespace std;
struct treenode {
    treenode* left;
    treenode* right;
    int data = -1;
};
string t;
treenode* buildtree(vector<int>& a, int& index) {
    if (index >= a.size()) return nullptr;

    // 如果当前值为0，表示空节点
    if (a[index] == 0) {
        index++;
        return nullptr;
    }

    // 创建当前节点
    treenode* root = new treenode();
    root->data = a[index];
    index++;

    // 递归构建左子树
    root->left = buildtree(a, index);

    // 递归构建右子树
    root->right = buildtree(a, index);

    return root;
}
vector<int>ans;
int mans;
int minl = 0xffffffff;
void dfs(treenode* node, vector<int>&path, int currentnum) {
    if (!node)return;
    currentnum += node->data;
    path.push_back(node->data);
    if (currentnum > mans||currentnum==mans&&minl>path.size()) {
        ans = path;
        minl = path.size();
        mans = currentnum;
    }
    if (node->left) {
        dfs(node->left, path, currentnum);
    }
    if (node->right) {
        dfs(node->right, path, currentnum);
    }
    path.pop_back();
}
void search(treenode* root) {
    if (!root)return;
    vector<int>path;
    dfs(root, path, 0);
    search(root->left);
    search(root->right);
}
int main() {
    getline(cin, t);
    stringstream ss(t);
    int a;
    vector<int>b;
    while (ss >> a) {
        b.push_back(a);
    }//去除空格
    int x = 0;
    treenode * root = buildtree(b, x);
    search(root);
    cout << mans<<endl;
    for (int i = 0; i < ans.size(); i++)cout << ans[i] << " ";
    return 0;
}
